package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import org.springframework.stereotype.Service;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.service.impl
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    /**
     * 频道分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo pageList(ChannelPageRequestDto dto) {
        //1. 设置分页参数
        IPage<AdChannel> pageInfo = new Page<>(dto.getPage(), dto.getSize());
        //2. 查询条件
        LambdaQueryWrapper<AdChannel> lqw = new LambdaQueryWrapper<>();
        //2.1 按名称模糊查询
        lqw.like(StringUtils.isNotEmpty(dto.getName()), AdChannel::getName, dto.getName());
        //2.2 按status查询
        lqw.eq(null != dto.getStatus(), AdChannel::getStatus, dto.getStatus());
        //  mybatis plus帮我们做了以下的活
        // 统计总数 select count(*) from 表名 where 条件
        // 分页查询 开始行号 long start = (page-1) * pageSize;
        // total > start
        // 查询结果集 select * from 表名 where 条件 limit start, pageSize

        // page方法时，pageInfo存入ThreadLocal



        //3. 分页查询
        page(pageInfo, lqw);
        //IPage<AdChannel> pageResult = page(pageInfo, lqw);

        //System.out.println("pageResult===========>" + pageResult);//全量类名@hashcode
        //System.out.println("pageInfo===========>" + pageInfo);//全量类名@hashcode
        //System.out.println("pageResult==pageInfo" + (pageInfo==pageResult));

        //4. 构建PageResultVo 返回
        return PageResultVo.pageResult(dto.getPage(), dto.getSize(), pageInfo.getTotal(), pageInfo.getRecords());
    }
}
